package com.example.zg.androidnewview.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.zg.androidnewview.R;

import java.util.List;

/**
 * 创建日期:2019/1/14 on 16:34
 * 描述：
 * 作者: zhougan
 */
public class MRecyclerAdapter extends RecyclerView.Adapter<MRecyclerAdapter.VH> {
    private static final String TAG = "MRecyclerAdapter";
    private List<String> data;//数据

    public MRecyclerAdapter() {
    }

    public MRecyclerAdapter(List<String> data) {
        this.data = data;
    }

    public void setData(List<String> data) {
        this.data = data;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.i(TAG, "onCreateViewHolder: 每个item布局");
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.item1,parent,false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        Log.i(TAG, "onBindViewHolder: 数据绑定"+position);
        holder.tvContext.setText(data.get(position));


    }

    //返回数据有多少个
    @Override
    public int getItemCount() {

        if(data!=null){
            return data.size();
        }
        return 0;
    }

    //自定义ViewHolder
    class VH extends RecyclerView.ViewHolder{
        private TextView tvContext;

        private VH(View itemView) {
            super(itemView);
            tvContext=itemView.findViewById(R.id.text_value);
        }
    }
}
